#ifndef __CONFIG_H__
#define __CONFIG_H__

#include "JsonUtil.hpp"
#include "FileUtil.hpp"
#include "log.hpp"
#include <mutex>
#include <memory>
#define CONFIG_FILE "cloud.conf"
namespace cloud
{
    class Config
    {
    private:
        static Config *_instance;
        static std::mutex _mutex;
    public:
        static Config *Getinstance()
        {
            if (Config::_instance == nullptr)
            {
                _mutex.lock();
                if (Config::_instance == nullptr)
                {
                    _instance = new Config;
                }
                _mutex.unlock();
            }
            return _instance;
        }
        int Get_hot_time()
        {
            return _hot_time;
        }
        int Get_server_port()
        {
            return _server_port;
        }
        std::string Get_server_ip()
        {
            return _server_ip;
        }
        std::string Get_download_prefix()
        {
            return _download_prefix;
        }
        std::string Get_packfile_suffix()
        {
            return _packfile_suffix;
        }
        std::string Get_pack_dir()
        {
            return _pack_dir;
        }
        std::string Get_back_dir()
        {
            return _back_dir;
        }
        std::string Get_backup_file()
        {
            return _backup_file;
        }
        class CGarbo
        {
            public:
            ~CGarbo()
            {
                if(Config::_instance)
                {
                    delete _instance;
                    _instance = nullptr;
                }
            }
        };
        static CGarbo cgarbo;
    private:
        Config()
        {
            ReadConfigFile();
        }
        int _hot_time;                // 热点时间
        int _server_port;             // 服务器端口号
        std::string _server_ip;       // 服务器ip
        std::string _download_prefix; // 下载的url前缀路径
        std::string _packfile_suffix; // 压缩后压缩包后缀
        std::string _pack_dir;        // 压缩包存放路径
        std::string _back_dir;        // 备份文件存放路径
        std::string _backup_file;     // 数据信息存放文件
        bool ReadConfigFile()
        {
            cloud::FileUtil fu(CONFIG_FILE);
            std::string body;
            if (fu.GetContent(&body) == false) // 拿到配置文件数据
            {
                logMassage(ERROR, "读取配置文件失败\n");
                return false;
            }
            Json::Value root;
            memset(&root, 0, sizeof(root));

            JsonUtil::DeSerialize(body, &root); // 拿到配置文件中的各个字段数据
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _backup_file = root["backup_file"].asString();
            return true;
        }
    };
    Config *Config::_instance = nullptr;
    Config::CGarbo cgarbo;
    std::mutex Config::_mutex;
}

#endif

/*
{
    "hot_time" : 30,
    "server_port" : 8888,
    "server_ip" : "120.26.57.122",
    "download_prefix" : "/download/",
    "packfile_suffix" : ".lz",
    "pack_dir" : "./packdir/",
    "back_dir" : "./backdir/",
    "backup_file" : "./cloud.dat"
}
*/